package com.nys.cqhttp.message.factory.impl;

import com.nys.cqhttp.mapper.*;
import com.nys.cqhttp.message.factory.IMessageHandler;
import com.nys.cqhttp.message.pojo.PhbPojo;
import com.nys.cqhttp.model.*;
import com.nys.cqhttp.param.Context;
import com.nys.cqhttp.param.MessageParam;
import com.nys.cqhttp.util.DateUtils;
import com.nys.cqhttp.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * DirectUrlMessageHandler
 *
 * @author nysheng
 * @version 1.0.0
 * @description 剪刀石头布
 * @date 2021/12/28 19:53
 */
@Slf4j
@Component
public class RPSMessageHandler implements IMessageHandler {
  @Autowired private TheMessageMapper theMessageMapper;
  @Autowired private MessageLogMapper messageLogMapper;
  @Autowired private GuessMessageMapper guessMessageMapper;
  @Autowired private GuessStateMapper guessStateMapper;
  private static RPSMessageHandler rPSMessageHandler;

  @Autowired private SysUserMapper sysUserMapper;
  private static String at = "[CQ:at,qq=";
  private static String at1 = "]";

  @PostConstruct
  public void init() throws Exception {
    rPSMessageHandler = this;
    theMessageMapper = this.theMessageMapper;
    sysUserMapper = this.sysUserMapper;
    messageLogMapper = this.messageLogMapper;
    guessStateMapper = this.guessStateMapper;
    guessMessageMapper = this.guessMessageMapper;
  }

  @Override
  public List<String> listHandler(Massage messagePojo, Context context) {
    return Arrays.asList(handler(messagePojo, context));
  }

  @Override
  public String handler(Massage messagePojo, Context context) {
    try {
      MessageParam messageParam = context.getMessageParam();
      List<String> list = Arrays.asList("剪刀", "石头", "布");
      String message = messageParam.getMessage();
      String senderId = messageParam.getSenderId();
      String selfId = messageParam.getSelfId();
      String res = "";
      String[] split = message.split("-");
      if (message.equals("游戏1")) {
        TheMessageExample theMessageExample = new TheMessageExample();
        TheMessageExample.Criteria criteria = theMessageExample.createCriteria();
        criteria.andJqrQqEqualTo(selfId);
        criteria.andUserQqEqualTo(senderId);
        criteria.andMessageLike(message);
        List<TheMessage> theMessages =
            rPSMessageHandler.theMessageMapper.selectByExample(theMessageExample);
        if (CollectionUtils.isEmpty(theMessages)) {
          TheMessage theMessage = new TheMessage();
          theMessage.setMessage(message);
          theMessage.setUserQq(senderId);
          theMessage.setJqrQq(selfId);
          rPSMessageHandler.theMessageMapper.insertSelective(theMessage);
        }
        res = messagePojo.getValue();
      } else if (list.contains(split[0])) {
        TheMessageExample theMessageExample = new TheMessageExample();
        TheMessageExample.Criteria theMessageExampleCriteria = theMessageExample.createCriteria();
        theMessageExampleCriteria.andJqrQqEqualTo(selfId);
        theMessageExampleCriteria.andUserQqEqualTo(senderId);
        theMessageExampleCriteria.andMessageLike("游戏1");
        List<TheMessage> theMessages =
            rPSMessageHandler.theMessageMapper.selectByExample(theMessageExample);
        if (CollectionUtils.isEmpty(theMessages)) {
          return "请先发送'游戏1'开始游戏";
        }
        MessageLogExample messageLogExample = new MessageLogExample();
        MessageLogExample.Criteria criteria1 = messageLogExample.createCriteria();
        criteria1.andJqrQqEqualTo(selfId);
        criteria1.andUserQqEqualTo(senderId);
        criteria1.andMessageEqualTo("游戏1-");
        criteria1.andSendDateGreaterThan(DateUtils.getNowDayStart());
        List<MessageLog> messageLogs =
            rPSMessageHandler.messageLogMapper.selectByExample(messageLogExample);
        if (messageLogs.size() >= 5) {
          return "今日次数已用完哦";
        }
        SysUser sysUser = new SysUser();
        Random random = new Random();

        //        String s = list.get(i1);
        String s1 = split[0];
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andJqrQqEqualTo(messageParam.getSelfId());
        criteria.andUserQqEqualTo(messageParam.getSenderId());
        List<SysUser> sysUsers = rPSMessageHandler.sysUserMapper.selectByExample(sysUserExample);
        if (CollectionUtils.isEmpty(sysUsers)) {
          sysUser.setJqrQq(messageParam.getSelfId());
          sysUser.setUserQq(messageParam.getSenderId());
          sysUser.setGold(0);
          rPSMessageHandler.sysUserMapper.insertSelective(sysUser);
          return "你还没有金币哦!";
        } else {
          sysUser = sysUsers.get(0);
          System.out.println(split[1]);
          if (Integer.valueOf(split[1]) > sysUser.getGold()) {
            return "你的金币不够哦!";
          }
        }
        int num = sysUser.getGold() >= 300 ? 6 : 3;
        //        int num = 3;
        int i1 = random.nextInt(num);
        System.out.println(i1);
        Integer i = 0;
        if (i1 == 1) {
          i = (Integer.valueOf(split[1]) / 5);
          res = "我出的也是" + s1 + "哦\n谢谢你陪我玩游戏,金币还给你,这些是你陪我玩游戏的酬劳" + i + "金币";

        } else if (s1.equals("剪刀")) {
          if (i1 != 0) {
            i = (-Integer.valueOf(split[1]));
            res = "我出的是石头\n我赢了,金币归我了!";
          } else {
            i = Integer.valueOf(split[1]) * 3;
            res = "我出的是布\n我输了[CQ:face,id=173],给你" + (Integer.valueOf(split[1]) * 3);
          }

        } else if (s1.equals("石头")) {
          if (i1 != 0) {
            i = (-Integer.valueOf(split[1]));
            res = "我出的是布\n我赢了,金币归我了!";
          } else {
            i = Integer.valueOf(split[1]) * 3;
            res = "我出的是剪刀\n我输了[CQ:face,id=173],给你" + (Integer.valueOf(split[1]) * 3);
          }
        } else if (s1.equals("布")) {
          if (i1 != 0) {
            i = (-Integer.valueOf(split[1]));
            res = "我出的是剪刀\n我赢了,金币归我了!";
          } else {
            i = Integer.valueOf(split[1]) * 3;
            res = "我出的是石头\n我输了[CQ:face,id=173],给你" + (Integer.valueOf(split[1]) * 3);
          }
        }
        MessageLog messageLog = new MessageLog();
        messageLog.setMessage("游戏1-");
        messageLog.setJqrQq(selfId);
        messageLog.setUserQq(senderId);
        messageLog.setSendDate(new Date());
        rPSMessageHandler.messageLogMapper.insertSelective(messageLog);
        sysUser.setGold(sysUser.getGold() + i);
        rPSMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);
      } else if (message.contains("猜数字")) {
        if (messageParam.getMessage().equals("猜数字")) {
          return at + messageParam.getSenderId() + at1 + "格式错误!猜数字-x x表示你要猜的数字.";
        }
        Integer num = Integer.valueOf(messageParam.getMessage().substring(4));
        if (num <= 0 || num > 10) {
          return at + messageParam.getSenderId() + at1 + "有效数字:1~10!";
        }
        MessageLogExample messageLogExample = new MessageLogExample();
        MessageLogExample.Criteria criteria1 = messageLogExample.createCriteria();
        criteria1.andJqrQqEqualTo(selfId);
        criteria1.andUserQqEqualTo(senderId);
        criteria1.andMessageEqualTo("猜数字");
        criteria1.andSendDateGreaterThan(DateUtils.getNowDayStart());
        List<MessageLog> messageLogs =
            rPSMessageHandler.messageLogMapper.selectByExample(messageLogExample);
        if (CollectionUtils.isEmpty(messageLogs)) {
          SysUserExample sysUserExample = new SysUserExample();
          SysUserExample.Criteria criteria = sysUserExample.createCriteria();
          criteria.andJqrQqEqualTo(messageParam.getSelfId());
          criteria.andUserQqEqualTo(messageParam.getSenderId());
          List<SysUser> sysUsers = rPSMessageHandler.sysUserMapper.selectByExample(sysUserExample);
          if (CollectionUtils.isEmpty(sysUsers)) {
            return at + messageParam.getSenderId() + at1 + "当前QQ未创建账号,请发送'签到'创建账号!";
          }
          SysUser sysUser = sysUsers.get(0);

          if (sysUser.getGold() < 10) {
            return at + messageParam.getSenderId() + at1 + "金币不足10";
          }
          GuessMessage guessMessage = new GuessMessage();
          guessMessage.setJqrQq(selfId);
          guessMessage.setUserQq(senderId);
          guessMessage.setSendDate(new Date());
          guessMessage.setNum(num);
          rPSMessageHandler.guessMessageMapper.insertSelective(guessMessage);
          GuessStateExample guessStateExample = new GuessStateExample();
          GuessStateExample.Criteria criteria2 = guessStateExample.createCriteria();
          criteria2.andJqrQqEqualTo(selfId);
          criteria2.andSendDateGreaterThan(DateUtils.getNowDayStart());
          List<GuessState> guessStates =
              rPSMessageHandler.guessStateMapper.selectByExample(guessStateExample);
          GuessState guessState = new GuessState();
          if (CollectionUtils.isEmpty(guessStates)) {
            GuessState guessState1 = rPSMessageHandler.guessStateMapper.lastData(selfId);
            int gold = 0;
            if (guessState1 != null) {
              Integer num1 = guessState1.getNum();
              gold = guessState1.getGold();
              if (num1 <= 0) {
                Random random = new Random();

                num1 = random.nextInt(10) + 1;
                guessState1.setNum(num1);
                rPSMessageHandler.guessStateMapper.updateByPrimaryKeySelective(guessState1);
              }
              List<GuessMessage> guessMessages =
                  rPSMessageHandler.guessMessageMapper.lastData(selfId, null, guessState1.getNum());
              if (CollectionUtils.isNotEmpty(guessMessages)) {
                gold = 0;
              }
            }
            guessState.setJqrQq(selfId);
            guessState.setSendDate(new Date());
            guessState.setGold(gold + 898);
            rPSMessageHandler.guessStateMapper.insertSelective(guessState);
          } else {
            MessageLogExample messageLogExample1 = new MessageLogExample();
            MessageLogExample.Criteria criteria4 = messageLogExample1.createCriteria();
            criteria4.andJqrQqEqualTo(selfId);
            criteria4.andMessageEqualTo("猜数字");
            criteria4.andSendDateGreaterThan(DateUtils.getNowDayStart());
            List<MessageLog> messageLogs1=
                    rPSMessageHandler.messageLogMapper.selectByExample(messageLogExample1);
            guessState = guessStates.get(0);
            if (messageLogs1.size()%20==0){
              guessState.setGold(guessState.getGold() + 1010);
            }else{
              guessState.setGold(guessState.getGold() + 10);
            }
            rPSMessageHandler.guessStateMapper.updateByPrimaryKeySelective(guessState);
          }
          sysUser.setGold(sysUser.getGold() - 10);
          rPSMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);
          MessageLog messageLog = new MessageLog();
          messageLog.setMessage("猜数字");
          messageLog.setUserQq(senderId);
          messageLog.setJqrQq(selfId);
          messageLog.setSendDate(new Date());
          rPSMessageHandler.messageLogMapper.insertSelective(messageLog);
          res = String.format(messagePojo.getValue(), guessState.getGold());
        } else {
          res = "今日已参与";
        }
      } else if (message.equals("领取奖励")) {
        MessageLogExample messageLogExample = new MessageLogExample();
        MessageLogExample.Criteria criteria1 = messageLogExample.createCriteria();
        criteria1.andJqrQqEqualTo(selfId);
        criteria1.andUserQqEqualTo(senderId);
        criteria1.andMessageEqualTo("领取奖励");
        criteria1.andSendDateGreaterThan(DateUtils.getNowDayStart());
        List<MessageLog> messageLogs =
            rPSMessageHandler.messageLogMapper.selectByExample(messageLogExample);
        if (CollectionUtils.isEmpty(messageLogs)) {
          GuessState guessState = rPSMessageHandler.guessStateMapper.lastData(selfId);
          if (guessState == null) {
            return at + messageParam.getSenderId() + at1 + "未中奖!";
          }
          Integer num = guessState.getNum();
          int gold = guessState.getGold();
          if (num <= 0) {
            Random random = new Random();
            num = random.nextInt(10) + 1;
            guessState.setNum(num);
            rPSMessageHandler.guessStateMapper.updateByPrimaryKeySelective(guessState);
          }
          List<GuessMessage> guessMessages =
              rPSMessageHandler.guessMessageMapper.lastData(selfId, senderId, num);
          if (CollectionUtils.isEmpty(guessMessages)) {
            return at + messageParam.getSenderId() + at1 + "昨日未中奖!";
          }
          GuessMessage guessMessage = guessMessages.get(0);
          if (guessMessage.getNum() == num) {
            List<GuessMessage> guessMessageList =
                rPSMessageHandler.guessMessageMapper.lastData(selfId, null, num);
            gold = (int) ((gold * 1.0) / guessMessageList.size());
            SysUserExample sysUserExample = new SysUserExample();
            SysUserExample.Criteria criteria = sysUserExample.createCriteria();
            criteria.andJqrQqEqualTo(messageParam.getSelfId());
            criteria.andUserQqEqualTo(messageParam.getSenderId());
            List<SysUser> sysUsers =
                rPSMessageHandler.sysUserMapper.selectByExample(sysUserExample);
            if (CollectionUtils.isEmpty(sysUsers)) {
              return at + messageParam.getSenderId() + at1 + "当前QQ未创建账号,请发送'签到'创建账号!";
            }
            SysUser sysUser = sysUsers.get(0);
            sysUser.setGold(sysUser.getGold() + gold);
            rPSMessageHandler.sysUserMapper.updateByPrimaryKeySelective(sysUser);
            MessageLog messageLog = new MessageLog();
            messageLog.setMessage("领取奖励");
            messageLog.setUserQq(senderId);
            messageLog.setJqrQq(selfId);
            messageLog.setSendDate(new Date());
            rPSMessageHandler.messageLogMapper.insertSelective(messageLog);
            return at + messageParam.getSenderId() + at1 + "领取成功,奖励:" + gold + "!";
          } else {
            return at + messageParam.getSenderId() + at1 + "昨日未中奖!";
          }
        } else {
          return at + messageParam.getSenderId() + at1 + "已领取!";
        }
      } else if (message.equals("今日奖池")) {
        GuessStateExample guessMessageExample = new GuessStateExample();
        GuessStateExample.Criteria criteria = guessMessageExample.createCriteria();
        criteria.andJqrQqEqualTo(selfId);
        criteria.andSendDateGreaterThan(DateUtils.getNowDayStart());
        List<GuessState> guessStates =
            rPSMessageHandler.guessStateMapper.selectByExample(guessMessageExample);
        GuessState guessState = new GuessState();
        if (CollectionUtils.isEmpty(guessStates)) {
          GuessState guessState1 = rPSMessageHandler.guessStateMapper.lastData(selfId);
          int gold = 0;
          if (guessState1 != null) {
            Integer num = guessState1.getNum();
            gold = guessState1.getGold();
            if (num <= 0) {
              Random random = new Random();
              num = random.nextInt(10) + 1;
              guessState1.setNum(num);
              rPSMessageHandler.guessStateMapper.updateByPrimaryKeySelective(guessState1);
            }
            List<GuessMessage> guessMessages =
                rPSMessageHandler.guessMessageMapper.lastData(selfId, null, num);
            if (CollectionUtils.isNotEmpty(guessMessages)) {
              gold = 0;
            }
          }
          guessState.setJqrQq(selfId);
          guessState.setSendDate(new Date());
          guessState.setGold(gold + 888);
          rPSMessageHandler.guessStateMapper.insertSelective(guessState);
        } else {
          guessState = guessStates.get(0);
        }
        return at + messageParam.getSenderId() + at1 + "当前奖池:" + guessState.getGold();
      } else if (message.equals("昨日开奖")) {
        GuessState guessState = rPSMessageHandler.guessStateMapper.lastData(selfId);
        if (guessState == null) {
          return "昨日未开始";
        }
        int num = guessState.getNum();
        if (num <= 0) {
          Random random = new Random();
          num = random.nextInt(10) + 1;
          guessState.setNum(num);
          rPSMessageHandler.guessStateMapper.updateByPrimaryKeySelective(guessState);
        }
        List<GuessMessage> guessMessages =
            rPSMessageHandler.guessMessageMapper.lastData(selfId, senderId, null);
        return at
            + messageParam.getSenderId()
            + at1
            + "昨日开奖数字:"
            + num
            + "!"
            + (CollectionUtils.isEmpty(guessMessages)
                ? "你昨日未参与!"
                : "你昨日投注:" + guessMessages.get(0).getNum());
      } else if (message.equals("昨日猜字")) {
        String str = "";
        for (int i = 1; i < 11; i++) {
          List<GuessMessage> guessMessages =
              rPSMessageHandler.guessMessageMapper.lastData(selfId, null, i);

          str +=
              ("\n"
                  + i
                  + "下注"
                  + (CollectionUtils.isEmpty(guessMessages) ? 0 : guessMessages.size())
                  + "人");
        }
        List<GuessMessage> guessMessages =
            rPSMessageHandler.guessMessageMapper.lastData(selfId, senderId, null);

        return at
            + messageParam.getSenderId()
            + at1
            + str
            + (CollectionUtils.isEmpty(guessMessages)
                ? "\n你昨日未下注!"
                : "\n你昨日下注数字:" + guessMessages.get(0).getNum());
      } else if (message.equals("中奖名单")) {
        GuessState guessState = rPSMessageHandler.guessStateMapper.lastData(selfId);
        List<GuessMessage> guessMessages =
            rPSMessageHandler.guessMessageMapper.lastData(selfId, null, guessState.getNum());
        String str = "昨日中奖名单";
        for (GuessMessage guessMessage : guessMessages) {
          str += "\n" + guessMessage.getUserQq();
        }
        return at + messageParam.getSenderId() + at1 + str;
      } else if (message.equals("今日猜字")) {
        String str = "";
        for (int i = 1; i < 11; i++) {
          GuessMessageExample guessMessageExample = new GuessMessageExample();
          GuessMessageExample.Criteria criteria = guessMessageExample.createCriteria();
          criteria.andJqrQqEqualTo(selfId);
          criteria.andNumEqualTo(i);
          criteria.andSendDateGreaterThan(DateUtils.getNowDayStart());
          List<GuessMessage> guessMessages =
              rPSMessageHandler.guessMessageMapper.selectByExample(guessMessageExample);
          str +=
              ("\n"
                  + i
                  + "下注"
                  + (CollectionUtils.isEmpty(guessMessages) ? 0 : guessMessages.size())
                  + "人");
        }
        GuessMessageExample guessMessageExample = new GuessMessageExample();
        GuessMessageExample.Criteria criteria = guessMessageExample.createCriteria();
        criteria.andJqrQqEqualTo(selfId);
        criteria.andUserQqEqualTo(senderId);
        criteria.andSendDateGreaterThan(DateUtils.getNowDayStart());
        List<GuessMessage> guessMessages =
            rPSMessageHandler.guessMessageMapper.selectByExample(guessMessageExample);

        return at
            + messageParam.getSenderId()
            + at1
            + str
            + (CollectionUtils.isEmpty(guessMessages)
                ? "\n你今日未下注!"
                : "\n你今日下注数字:" + guessMessages.get(0).getNum());
      } else if (message.equals("中奖排行榜")) {
        List<PhbPojo> messages = rPSMessageHandler.messageLogMapper.getPhb();
        String str = at + messageParam.getSenderId() + at1+"中奖排行榜";
        for (int i = 0; i < messages.size(); i++) {
          str +=
              ("\n"
                  + (i+1)
                  + "."
                  + messages.get(i).getUserQq()
                  + "/"
                  + messages.get(i).getCount()
                  + "次");
        }
        return str;
      }
      return at + messageParam.getSenderId() + at1 + res;
    } catch (Exception e) {
      e.printStackTrace();
      return e.getMessage();
    }
  }
}
